<?php

namespace App\Http\Controllers\Wechat;

use App\Events\WechatAuth;
use App\Jobs\SaveSubscribeUser;
use App\Model\User;
use App\Repository\UserRepository;
use EasyWeChat\Server\Guard;
use Config;

/**
 * @property Guard server
 */

class HomeController extends BaseController
{

    /**
     * 微信响应方法
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
     */
    public function index()
    {
        $this->server->setMessageHandler('App\Handlers\WechatHandler::run');

        $this->server->serve()->send();
    }

    /**
     * 更新微信菜单
     * @return mixed
     */
    public function menu()
    {
        return $this->menu->add(Config::get('menu'));
    }

    public function callback()
    {
        $wechatUser = $this->app->oauth->user();

        event(new WechatAuth($wechatUser->id));

        return redirect(session('targetUrl'));
    }
}
